<?php
namespace Common\Model;
use Think\Model;

/**
 * Class CsdtModel
 * @package Common\Model
 * 调蓄池表
 */
class CsdtModel extends BaseModel{
    public $pix = "";
    public $fieldsStr = '*';
    public $orderStr = 'ps_name desc';

    public function field_array(){
        /**
         * type:
         * 1，文本框
         * 2，下拉框
         * 3，数值查询，带条件
         * 4，日期查询，带条件
         */
        $array = array(
            'base' => array(
                'text' => '基本信息',
                'children'=>array(
                    array('field'=>'ps_id','text' => '排水调蓄池标识码','type'=>1),
                    array('field'=>'ps_sysid','text' => '排水系统编码','type'=>1),
                    array('field'=>'ps_name','text' => '调蓄池名称','type'=>1),
                    array('field'=>'ps_catg3','text' => '调蓄池基类','type'=>2,  'option_text'=>pump_station_base_types_option()),
                    array('field'=>'ps_catg1','text' => '调蓄池大类','type'=>2, 'option_text'=>pump_station_types_option()),
                    array('field'=>'ps_catg2','text' => '调蓄池小类','type'=>2, 'option_text'=>pump_station_types_second_option()),
                    array('field'=>'ps_scale','text' => '调蓄池规模','type'=>2, 'option_text'=>pump_station_scope_option()),
                    array('field'=>'ps_addr','text' => '调蓄池地址','type'=>1),
                    array('field'=>'ps_region','text' => '行政区划','type'=>2,  'option_text'=>pump_station_area_option()),
                    array('field'=>'ps_svc_scope','text' => '服务范围','type'=>1),
                    array('field'=>'ps_area','text' => '占地面积(平方米)','type'=>3,'condition'=>get_condition_array()),
                    array('field'=>'ps_area','text' => '占地面积','type'=>3,'condition'=>get_condition_array()),
                    array('field'=>'ps_p_num','text' => '总水泵台数','type'=>3,'condition'=>get_condition_array()),
                    array('field'=>'ps_bkp_num','text' => '备用水泵台数','type'=>3,'condition'=>get_condition_array()),
                    array('field'=>'ps_drain_amt','text' => '总排水能力','type'=>3,'condition'=>get_condition_array()),
                    array('field'=>'ps_dev_cap','text' => '总装机容量','type'=>3,'condition'=>get_condition_array()),
                    array('field'=>'ps_power_amt','text' => '调蓄池总功率','type'=>3,'condition'=>get_condition_array()),
                    array('field'=>'ps_rwp_num','text' => '雨水泵台数','type'=>3,'condition'=>get_condition_array()),
                    array('field'=>'ps_bkrwp_num','text' => '备用雨水泵台数','type'=>3,'condition'=>get_condition_array()),
                    array('field'=>'ps_rwdrain_amt','text' => '雨水总排水能力','type'=>3,'condition'=>get_condition_array()),
                    array('field'=>'ps_rwdrain_dsgn','text' => '设计雨水排水能力','type'=>3,'condition'=>get_condition_array()),
                    array('field'=>'ps_rwdev_cap','text' => '雨水装机容量','type'=>3,'condition'=>get_condition_array()),
                    array('field'=>'ps_rwpower_amt','text' => '雨水调蓄池总功率','type'=>3,'condition'=>get_condition_array()),
                    array('field'=>'ps_sp_num','text' => '污水泵台数','type'=>3,'condition'=>get_condition_array()),
                    array('field'=>'ps_bksp_num','text' => '备用污水泵台数','type'=>3,'condition'=>get_condition_array()),
                    array('field'=>'ps_sdrain_amt','text' => '污水总排水能力','type'=>3,'condition'=>get_condition_array()),
                    array('field'=>'ps_sdrain_dsgn','text' => '设计污水排水能力','type'=>3,'condition'=>get_condition_array()),
                    array('field'=>'ps_sdev_cap','text' => '污水装机容量','type'=>3,'condition'=>get_condition_array()),
                    array('field'=>'ps_spower_amt','text' => '污水调蓄池总功率','type'=>3,'condition'=>get_condition_array()),
                    array('field'=>'ps_ro_dsgn','text' => '设计综合径流系数','type'=>3,'condition'=>get_condition_array()),
                    array('field'=>'ps_rtp_dsgn','text' => '设计暴雨重现期','type'=>3,'condition'=>get_condition_array()),
                    array('field'=>'ps_up_p','text' => '上游调蓄池名称','type'=>1),
                    array('field'=>'ps_down_p','text' => '下游调蓄池名称','type'=>1),
                    array('field'=>'ps_to_riv','text' => '排入水体','type'=>1),
                    array('field'=>'ps_w_purific','text' => '下游水质净化厂','type'=>1),
                    array('field'=>'ps_int_num','text' => '进水管渠数量','type'=>3,'condition'=>get_condition_array()),
                    array('field'=>'ps_out_num','text' => '出水管数量','type'=>3,'condition'=>get_condition_array()),
                ),
            ),
            'ps_status' => array(
                'text' =>'设施状态',
                'children' => array(
                    array('field'=>'ps_sts','text' => '设施状态','type'=>2,  'option_text'=>pump_station_status_option()),
                    array('field'=>'ps_run_typ','text' => '运转类型','type'=>2,  'option_text'=>ps_run_typ()),
                    array('field'=>'ps_guard_typ','text' => '值守类型','type'=>2,  'option_text'=>ps_guard_typ()),
                    array('field'=>'ps_shift_sys','text' => '轮班制度','type'=>2,  'option_text'=>ps_shift_sys()),
                )
            ),
            'belong_info' => array(
                'text' =>'权属信息',
                'children' => array(
                    array('field'=>'ps_ename','text' => '所属工程','type'=>1),
                    array('field'=>'ps_bulid_dept','text' => '建设单位','type'=>1),
                    array('field'=>'ps_dsgn_dept','text' => '设计单位','type'=>1),
                    array('field'=>'ps_constr_dept','text' => '施工单位','type'=>1),
                    array('field'=>'ps_surv_dept','text' => '监理单位','type'=>1),
                    array('field'=>'ps_prop_dept','text' => '产权单位','type'=>1),
                    array('field'=>'ps_mt_dept','text' => '维护单位','type'=>1),
                    array('field'=>'ps_tel','text' => '联系电话','type'=>1),
                    array('field'=>'ps_finish_dt','text' => '竣工日期','type'=>4,'condition'=>get_condition_array()),
                    array('field'=>'ps_svc_dt','text' => '投运日期','type'=>4,'condition'=>get_condition_array()),
                    array('field'=>'ps_recon_dt','text' => '改建日期','type'=>4,'condition'=>get_condition_array()),
                )
            ),
            'device' => array(
                'text' =>'配套设备',
                'children' => array(
                    array('field'=>'dev_gate','text' => '闸门','type'=>2,  'option_text'=>is_have()),
                    array('field'=>'dev_ipc','text' => '工控系统','type'=>2,  'option_text'=>is_have()),
                    array('field'=>'dev_sec_sys','text' => '安防系统','type'=>2,  'option_text'=>is_have()),
                    array('field'=>'dev_screemach','text' => '格栅除污机','type'=>2,  'option_text'=>is_have()),
                    array('field'=>'dev_conv','text' => '输送机','type'=>2,  'option_text'=>is_have()),
                    array('field'=>'dev_press','text' => '压榨机','type'=>2,  'option_text'=>is_have()),
                    array('field'=>'dev_deodorizer','text' => '除臭设备','type'=>2,  'option_text'=>is_have()),
                    array('field'=>'dev_vent','text' => '通风设备','type'=>2,  'option_text'=>is_have()),
                    array('field'=>'dev_hoist','text' => '起吊设备','type'=>2,  'option_text'=>is_have()),
                    array('field'=>'dev_gen','text' => '发电机','type'=>2,  'option_text'=>is_have()),
                    array('field'=>'dev_valve','text' => '阀门','type'=>2,  'option_text'=>is_have()),
                    array('field'=>'dev_penstock','text' => '河闸','type'=>2,  'option_text'=>is_have()),
                )
            ),
            'data' => array(
                'text' =>'数据信息',
                'children' => array(
                    array('field'=>'ps_groprc_dl','text' => '竣工决算价','type'=>3,'condition'=>get_condition_array()),
                    array('field'=>'ps_mapno','text' => '图幅编号','type'=>1),
                    array('field'=>'ps_datasrc','text' => '数据来源','type'=>2,  'option_text'=>ps_datasrc()),
                    array('field'=>'ps_rpt_dept','text' => '填报单位','type'=>1),
                )
            ),
        );
        return $array;
    }

    public function get_list_use_page($map=array(), $cur_page, $rows_num){
        $map_filter = filter_query_map_get_order_str($map, $this->orderStr);
        $map = $map_filter['map'];
        $order_str = $map_filter['order_str'];
        $map = filter_field_array($this->field_array(), $map);
        $count = M('Csdt')->where($map)->count();
        $list = $this->where($map)->order($order_str)->page($cur_page.",$rows_num")->select();
        $pump_model = D('CsdtPump');
        $machine_model = D('CsdtMachine');
        $gate_model = D('CsdtGate');
        $temp_status = 0;
        foreach($list as $k => $v){
            $list[$k]['ps_catg1'] = pump_station_types_option($list[$k]['ps_catg1'], false);
            $list[$k]['ps_catg2'] = pump_station_types_second_option($list[$k]['ps_catg2'], false);
            $list[$k]['ps_catg3'] = pump_station_base_types_option($list[$k]['ps_catg3'], false);
            $list[$k]['ps_scale'] = pump_station_scope_option($list[$k]['ps_scale'], false);
            $list[$k]['ps_region'] = pump_station_area_option($list[$k]['ps_region'], false);
            $list[$k]['ps_sts'] = pump_station_status_option($list[$k]['ps_sts'], false);
            $list[$k]['ps_run_typ'] = ps_run_typ($list[$k]['ps_run_typ'], false);
            $list[$k]['ps_guard_typ'] = ps_guard_typ($list[$k]['ps_guard_typ'], false);
            $list[$k]['charge_person'] = filter_id_to_ralname($list[$k]['ps_opr']);
            $pump_status = $pump_model->q_s($v['id']);
            $mac_status = $machine_model->q_s($v['id']);
            $gate_status = $gate_model->q_s($v['id']);
            if($pump_status==-1||$mac_status==-1||$gate_status==-1){
                $temp_status = -2;
            }else if($gate_status==1){
                $temp_status = 1;
            }else{
                $temp_status = 0;
            }
            $list[$k]['status'] = station_status_option($temp_status, false);
            $temp_status = 0;
        }
        $footer = array(array('ps_id'=>'调蓄池总数', 'status'=>$count));
        return array('total'=>$count, 'list'=>$list, 'map'=>$map, 'footer'=>$footer);
    }

    public function gen_search($map, $page, $rows_num){
        $pump_model = D('CsdtPump');
        $machine_model = D('CsdtMachine');
        $gate_model = D('CsdtGate');
        $status = '';
        if(isset($map['status'])){
            $status = str_replace('%', '', $map['status'][1]);
            unset($map['status']);
        }
        //查询出所有泵站
        $list = $this->where($map)->order($this->orderStr)->select();
        $temp_status = 0;
        $result = array();
        foreach($list as $k => $v){
            $pump_status = $pump_model->q_s($v['id']);
            $mac_status = $machine_model->q_s($v['id']);
            $gate_status = $gate_model->q_s($v['id']);
            if($pump_status==-1||$mac_status==-1||$gate_status==-1){
                $temp_status = -2;
            }else if($gate_status==1){
                $temp_status = 1;
            }else{
                $temp_status = 0;
            }
            if($status == '' || $temp_status == $status){
                $list[$k]['ps_catg1'] = pump_station_types_option($list[$k]['ps_catg1'], false);
                $list[$k]['ps_catg2'] = pump_station_types_second_option($list[$k]['ps_catg2'], false);
                $list[$k]['ps_catg3'] = pump_station_base_types_option($list[$k]['ps_catg3'], false);
                $list[$k]['ps_scale'] = pump_station_scope_option($list[$k]['ps_scale'], false);
                $list[$k]['ps_region'] = pump_station_area_option($list[$k]['ps_region'], false);
                $list[$k]['ps_sts'] = pump_station_status_option($list[$k]['ps_sts'], false);
                $list[$k]['ps_run_typ'] = ps_run_typ($list[$k]['ps_run_typ'], false);
                $list[$k]['ps_guard_typ'] = ps_guard_typ($list[$k]['ps_guard_typ'], false);
                $list[$k]['status'] = station_status_option($temp_status, false);
                $list[$k]['charge_person'] = filter_id_to_ralname($list[$k]['ps_opr']);
                $result[] = $list[$k];
            }
            $temp_status = 0;
        }
        $list = array_slice($result, ($page-1)*$rows_num, $rows_num);
        $footer = array(array('ps_id'=>'调蓄池总数', 'status'=>count($result)));
        return array('total'=>count($result), 'rows'=>$list, 'footer'=>$footer);
    }

    public function filter_all(){
        $list = $this->getAll();
        foreach($list as $k => $v){
            $list[$k]['ps_opr'] = filter_id_to_ralname($list[$k]['ps_opr']);
        }
        return $list;
    }

    public function query_ids_by_opr($query_str){
        $map['ps_opr'] = array('like', '%'.$query_str.'%');
        $list = $this->where($map)->order($this->orderStr)->select();
        $push = array();
        foreach($list as $k => $v){
            $push[] = $v['id'];
        }
        return $push;
    }

    public function count($start_time, $end_time){
        $pump_model = D('CsdtPump');
        $machine_model = D('CsdtMachine');
        $waterele_model = D('CsdtWaterele');
        $stations = $this->getAll();
        $return_array = array();
        $index = 0;
        foreach($stations as $v){
            $pump_result = $pump_model->count($v['id'], $start_time, $end_time);
            $mac_result = $machine_model->count($v['id'], $start_time, $end_time);
            $ele_result = $waterele_model->count($v['id'], $start_time, $end_time);
            $return_array[$index]['date'] = substr($start_time,0,10).'/'.substr($end_time,0,10);
            $return_array[$index]['pump'] = $v['ps_name'];
            $return_array[$index]['index'] = 0;
            $return_array[$index]['water_time'] = $pump_result['time']?$pump_result['time']:0;
            $return_array[$index]['time'] = $pump_result['time']?$pump_result['time']:0;
            $return_array[$index]['flow'] = $pump_result['flow']?$pump_result['flow']:0;
            $return_array[$index]['mac_time'] = $mac_result['time']?$mac_result['time']:0;
            $return_array[$index]['ele'] = $ele_result['water'];
            $return_array[$index]['water'] = $ele_result['ele'];
            $index++;
        }
        return $return_array;
    }

    public function get_csdt_station_tree(){
        $tree = array();
        $top = array();
        $top['id'] = '-1';
        $top['text'] = '全部水调蓄池';
        $top['children'] = array();
        $stations = $this->getAll();
        foreach($stations as $sta){
            $temp_sta = array();
            $temp_sta['id'] = $sta['id'];
            $temp_sta['text'] = '<a href="#" onclick="editCsdt('.$sta['id'].')">'.$sta['ps_name'].'</a>';
            $top['children'][] = $temp_sta;
        }
        $tree[] = $top;
        return $tree;
    }

    public function deleteArray($ids=array()){
        $return = '';
        if(!is_array($ids)){
            $ids = array_filter(explode(',', $ids));
        }
        $fys_models = array(
            'CsdtPump',
            'CsdtMachine',
            'CsdtWaterele',
            'CsdtCollectingBasin',
            'CsdtForebay',
            'CsdtInlet',
            'CsdtOutletSum',
            'CsdtOutletPipe',
            'CsdtValve',
            'CsdtGate',
            'CsdtConveyor',
            'CsdtPress',
            'CsdtHoist',
            'CsdtCharging',
            'CsdtDynamo',
            'CsdtTransFormer',
        );
        foreach($ids as $id){
            foreach($fys_models as $fy_model){
                $map['ps_id'] = array('eq', $id);
                $temps = M($fy_model)->where($map)->select();
                $fy_ids = array();
                foreach($temps as $fy){
                    $fy_ids[] = $fy['id'];
                }
                D($fy_model)->deleteArray($fy_ids);
            }
            $return = $this->delete($id);
        }
        return $return;
    }
}